package com.cskaoyan.controller;

import com.cskaoyan.bean.PO.MarketStorage;
import com.cskaoyan.bean.common.BasePageInfo;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.service.AdminStorageService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;

import static net.sf.jsqlparser.parser.feature.Feature.update;

/**
 * @ClassName AdminStorageController
 * @Description
 * @Author Lxz
 * @Date 2022-8-13 8:56
 */
@RestController
@RequestMapping("admin/storage")
public class AdminStorageController {
    @Autowired
    AdminStorageService adminStorageService;

    /*
     * todo-Lxz 魔法值+1，后续放配置文件
     * @Date  22:26
     **/
    private String[] arrsuffix = {"image/png","image/jpg","image/jpeg","image/bmp","image/gif"};


    @RequiresPermissions("admin:storage:list")
    @RequestMapping("list")
    public BaseRespVo list(String name,String key,BasePageInfo info) {
        CommonData list = adminStorageService.list(name,key,info);
        BaseRespVo responseJson = BaseRespVo.ok(0, list, "成功");
        return responseJson;
    }


    @RequiresPermissions("admin:storage:create")
    @RequestMapping("create")
    public BaseRespVo create(MultipartFile file, HttpServletRequest request) throws IOException {
        String originalFilename = file.getOriginalFilename();

        /*
         * todo-Lxz 这里用的是UUID随机生成 ，后续再看要不要用加密
         * @Date  22:26
         **/
        String random = UUID.randomUUID().toString().replace("-", "");
        String[] split = originalFilename.split("\\.");
        String storageKey = random + "." + split[split.length - 1];

        String contentType = file.getContentType();
        long size = file.getSize();
        /*
         * todo-Lxz 后期再改
         * @Date  22:26
         **/
        String url = "http://localhost:8083/pic/" + storageKey;

        // 存到本地磁盘中
        String locationPath = "c:/spring/";
        File file1 = new File(locationPath,storageKey);
        file.transferTo(file1);

        List<String> types = Arrays.asList(arrsuffix);
        if (!types.contains(contentType)) {
            return BaseRespVo.ok(502,null,"不支持该类型文件上传");
        }


        MarketStorage marketStorage = adminStorageService.create(storageKey, originalFilename, contentType, size,url);
        return BaseRespVo.ok(0,marketStorage,"成功");
    }

    @RequiresPermissions("admin:storage:update")
    @RequestMapping("update")
    public BaseRespVo update(@RequestBody MarketStorage marketStorage) {
        String name = marketStorage.getName();
        if (StringUtils.isEmpty(name) && "".equals(name)){
            return BaseRespVo.custom(502, update, "输入不可为空");
        }
        if (name.length() > 60) {
            return BaseRespVo.custom(502, update, "输入过长，请重新输入");
        }
        MarketStorage update = adminStorageService.update(marketStorage);
        return BaseRespVo.ok(0, update, "成功");
    }

    @RequiresPermissions("admin:storage:delete")
    @RequestMapping("delete")
    public BaseRespVo delete(@RequestBody MarketStorage marketStorage) {
        adminStorageService.delete(marketStorage);
        return BaseRespVo.ok(0, null, "成功");
    }
}
